package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageableEntitiesConverter;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.stock.Stock;
import com.ruoyi.web.pojo.dto.stock.StockExcel;
import com.ruoyi.web.pojo.dto.stock.StockRep;
import com.ruoyi.web.pojo.dto.stock.StockVo;
import com.ruoyi.web.pojo.request.stock.StockAddReq;
import com.ruoyi.web.service.stock.IStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(value = "库存管理", tags = {"库存管理"})
@RestController
@RequestMapping("/stock")
public class StockController extends BaseController
{

    private final IStockService stockService;

    public StockController(IStockService stockService) {
        this.stockService = stockService;
    }

    /**
     * 查询库存列表
     */
    @ApiOperation("查询库存列表")
    @GetMapping("/list")
    public TableDataInfo<StockRep> list(Stock stock)
    {
        startPage();
        List<StockVo> list = stockService.selectStockList(stock);
        if (CollectionUtils.isEmpty(list)){
            return  getDataTable(new ArrayList<>());
        }
        List<StockRep> collect = PageableEntitiesConverter.toVos(list,this::toReq);
        return getDataTable(collect);
    }

    private List<StockRep> toReq(List<StockVo> list){
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(StockRep::new).collect(Collectors.toList());
    }

    /**
     * 获取库存详细信息
     */
    @ApiOperation("获取库存详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        StockVo stock = stockService.selectStockById(id);
        if (Objects.isNull(stock)){
            return success();
        }
        return success(new StockRep(stock));
    }

    /**
     * 新增库存
     */
    @ApiOperation("新增库存")
    @RateLimiter(time = 1,count = 1)
    @PostMapping("insert")
    public AjaxResult add(@RequestBody @Valid StockAddReq stock)
    {
        return toAjax(stockService.insertStock(Stock.toStock(stock)));
    }

    /**
     * 导出库存明显列表
     */
    @ApiOperation("导出库存明显列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Stock stock) throws Exception {
        List<StockVo> list = stockService.selectStockList(stock);
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        List<StockExcel> collect = list.stream().map(StockExcel::new).collect(Collectors.toList());
        ExcelUtil<StockExcel> util = new ExcelUtil<>(StockExcel.class);
//        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("库存明显数据"+ DateUtils.datePath() +".xlsx", StandardCharsets.UTF_8.name()));
        util.exportExcel(response, collect, "库存明显数据");
    }

    /**
     * 删除库存
     */
    @ApiOperation("删除库存")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(stockService.deleteStockById(id));
    }
}
